﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace Portal
{
    public class ActionPermission
    {



        public string Controller { get; set; }

        public string Action { get; set; }

        public string Method { get; set; }

        public string Area { get; set; }

        public string Description { get; set; }

        public bool IsPage { get; set; }



        public static IList<ActionPermission> GetAllActionByAssembly()
        {
            var assembly = PermissionConfig.AssemblyList;
            var baseType = PermissionConfig.BaseTypeList;
            var result = new List<ActionPermission>();
            foreach (var item in assembly)
            {
                var types = Assembly.Load(item).GetTypes();
                foreach (var type in types)
                {
                    if (type.BaseType != null && baseType.Contains(type.BaseType.Name))//如果是Controller
                    {
                        var area = string.Empty;
                        var members = type.GetMethods();
                        var typeFullName = type.FullName;
                        if (typeFullName != null)
                        {
                            MatchCollection mc = PermissionConfig.AreaRegex.Matches(typeFullName);
                            if (mc.Count > 0)
                                area = mc[0].Value;
                        }
                        foreach (var member in members)
                        {
                            if (member.ReturnType.Name == "ActionResult")//如果是Action
                            {
                                if (member.DeclaringType != null)
                                {
                                    var ap = new ActionPermission
                                                 {
                                                     Action = member.Name,
                                                     Controller =
                                                         member.DeclaringType.Name.Substring(0,
                                                                                             member.DeclaringType.Name.
                                                                                                 Length - 10),
                                                     Area = area
                                                 };


                                    // 去掉“Controller”后缀
                                    object[] attrs = member.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
                                    if (attrs.Length > 0)
                                    {
                                        var descriptionAttribute = attrs[0] as System.ComponentModel.DescriptionAttribute;
                                        if (descriptionAttribute != null)
                                            ap.Description = descriptionAttribute.Description;
                                    }
                                    object[] methodGet = member.GetCustomAttributes(typeof(System.Web.Mvc.HttpGetAttribute), true);
                                    if (methodGet.Length > 0)
                                    {
                                        ap.Method = "GET";
                                    }
                                    object[] methodPost = member.GetCustomAttributes(typeof(System.Web.Mvc.HttpPostAttribute), true);
                                    if (methodPost.Length > 0)
                                    {
                                        ap.Method = "POST";
                                    }
                                    object[] isPage = member.GetCustomAttributes(typeof(ViewPageAttribute), true);
                                    ap.IsPage = isPage.Length > 0;
                                    result.Add(ap);
                                }
                            }

                        }
                    }
                }
            }

            return result;
        }

    }
}
